package com.zlll.winner.service.media.async;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.live.v20180801.LiveClient;
import com.tencentcloudapi.live.v20180801.models.CreateLiveRecordRequest;
import com.tencentcloudapi.live.v20180801.models.CreateLiveRecordResponse;
import com.tencentcloudapi.live.v20180801.models.DeleteLiveRecordRequest;
import com.tencentcloudapi.live.v20180801.models.DeleteLiveRecordResponse;
import com.zlll.winner.business.model.media.MediaNewList;
import com.zlll.winner.mapper.media.MediaNewListMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Service
@Async
@Slf4j
public class MediaNewListAsyncService {

    @Resource
    private MediaNewListMapper mediaNewListMapper;

    @Async
    public void createLiveRecord(Long mediaNewListId,String startTime,String endTime){
        MediaNewList mediaNewList = mediaNewListMapper.selectById(mediaNewListId);
        long recordInterval = mediaNewList.getShowEndTime()-mediaNewList.getShowBeginTime();
        log.error("recordInterval===>{}===>{}",recordInterval,(recordInterval/1000L));
        LiveClient client =createLiveClient();
        CreateLiveRecordRequest request = new CreateLiveRecordRequest();
        request.setStreamName("LHT");
        try {
            request.setStartTime(URLEncoder.encode(startTime, "utf-8").toLowerCase());
            request.setEndTime(URLEncoder.encode(endTime, "utf-8").toLowerCase());
            request.setRecordType("audio");
            request.setFileFormat("mp3");
            request.setHighlight(0);
            String streamParam = URLEncoder.encode("record_interval="+(recordInterval/1000L)+"&storage_time="+(recordInterval/1000L), "utf-8");
            request.setStreamParam(streamParam);
            log.error("创建录制任务请求内容====》{}",CreateLiveRecordRequest.toJsonString(request));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        CreateLiveRecordResponse resp = null;
        try {
            resp = client.CreateLiveRecord(request);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        log.error("创建录制任务返回内容====》{}",CreateLiveRecordRequest.toJsonString(resp));

        mediaNewList.setTaskId(resp.getTaskId().longValue());
        mediaNewList.setRequestId(resp.getRequestId());
        mediaNewListMapper.updateById(mediaNewList);
    }

    private LiveClient createLiveClient(){
        Credential cred = createCredential();
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("live.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        return new LiveClient(cred, "ap-guangzhou", clientProfile);
    }

    private Credential createCredential(){
        return new Credential("AKIDaz95wYPLEsGOHlsziCTWXK3XpreCnXL7", "g7WjiOq58xXt3y1vfdNYZP0NRJ7i387r");
    }

    @Async
    public void deleteLiveRecord(Long taskId){
        LiveClient client =createLiveClient();
        DeleteLiveRecordRequest request = new DeleteLiveRecordRequest();
        request.setStreamName("LHT");
        request.setTaskId(taskId.intValue());
        DeleteLiveRecordResponse resp = null;
        try {
            resp = client.DeleteLiveRecord(request);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        log.error("删除录制任务返回内容====》{}",CreateLiveRecordRequest.toJsonString(resp));
    }
}
